﻿using Newtonsoft.Json;

namespace HuaRunInterface.Models
{
    /// <summary>
    /// 统一返回数据对象结构
    /// </summary>
    public class ReturnData
    {
        /// <summary> </summary>
        public static string STATE_SUCCESS = "success";
        /// <summary> </summary>
        public static string STATE_FAILED = "failed";

        /// <summary> </summary>
        public ReturnData()
        {
            State = STATE_SUCCESS;
            Message = "";
            Data = null;
            Total = null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        public ReturnData(object? d)
        {
            State = STATE_SUCCESS;
            Message = "";
            Data = d;
            Total = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="total"></param>
        public ReturnData(object? page, int total)
        {
            State = STATE_SUCCESS;
            Message = "";
            Data = page;
            Total = total;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isOk"></param>
        /// <param name="errMsg"></param>
        /// <param name="d"></param>
        /// <param name="total"></param>
        public ReturnData(bool isOk, string errMsg, object? d = null, int? total = null)
        {
            State = isOk ? STATE_SUCCESS : STATE_FAILED;
            Message = errMsg;
            Data = d;
            Total = total;
        }

        /// <summary> 
        /// 状态 
        /// <para>success - 成功</para>
        /// <para>failed - 失败</para>
        /// </summary>
        [JsonProperty("State")]
        public string State { get; set; } = STATE_SUCCESS;
        /// <summary> 消息 </summary>
        [JsonProperty("Message")]
        public string Message { get; set; } = "";
        /// <summary> 数据 </summary>
        [JsonProperty("Data", NullValueHandling = NullValueHandling.Ignore)]
        public object? Data { get; set; } = null;
        /// <summary> 总记录数量（Data为集合时） </summary>
        [JsonProperty("Total", NullValueHandling = NullValueHandling.Ignore)]
        public int? Total { get; set; } = null;
    }

    /// <summary>
    /// 统一返回数据对象结构
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ReturnData<T>: ReturnData
    {
        /// <summary>
        /// 
        /// </summary>
        /// <summary> 数据 </summary>
        [JsonProperty("Data", NullValueHandling = NullValueHandling.Ignore)]
        new public T? Data { get; set; } = default;

        /// <summary>  </summary>
        public ReturnData()
        {
            State = STATE_SUCCESS;
            Message = "";
            Data = default;
            Total = null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="d"></param>
        public ReturnData(T d) 
            : base(d)
        {
            Data = d;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="total"></param>
        public ReturnData(T page, int total)
            :base(page, total)
        {
            Data = page;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isOk"></param>
        /// <param name="errMsg"></param>
        /// <param name="d"></param>
        /// <param name="total"></param>
        public ReturnData(bool isOk, string errMsg, T? d = default, int? total = null)
            : base(isOk, errMsg, d, total)
        {
            Data = d;
        }
    }

    /// <summary>
    /// 统一返回数据集合结构
    /// </summary>
    public class ReturnList<T> : ReturnData<List<T>>
    {
        /// <summary>
        /// 
        /// </summary>
        public ReturnList()
        {
            State = STATE_SUCCESS;
            Message = "";
            Data = null;
            Total = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="total"></param>
        public ReturnList(List<T> page, int total)
        {
            State = STATE_SUCCESS;
            Message = "";
            Data = page;
            Total = total;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isOk"></param>
        /// <param name="errMsg"></param>
        /// <param name="d"></param>
        /// <param name="total"></param>
        public ReturnList(bool isOk, string errMsg, List<T>? d = null, int? total = null)
        {
            State = isOk ? STATE_SUCCESS : STATE_FAILED;
            Message = errMsg;
            Data = d;
            Total = total;
        }
    }

    public class ResBaseModel2
    {
        public ResBaseModel2()
        {
            State = "failed";
            Data = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
        }
        public string State { get; set; }
        public string Message { get; set; }
        public string Data { get; set; }
    }

    public class ResBaseModel3
    {
        public ResBaseModel3()
        {
            State = "failed";
            Data = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
        }
        public string State { get; set; }
        public string Message { get; set; }
        public object Data { get; set; }
    }
}
